// const puppeteer = require('puppeteer')
// let cookieString = `NAME=zs`
// const addCookies = async (cookies_str,page,domain) => {
//   let cookies = cookies_str.split(';').map(pair => {
//     let name = pair.trim().slice(0,pair.trim().indexOf('='))
//     let value = pair.trim().slice(pair.trim().indexOf('=')+1)
//     return {name, value, domain}
//   })
//   await Promise.all(cookies.map(pair => {
//     console.log(pair);
//     return page.setCookie(pair)
//   }))
// }

const puppeteer = require('puppeteer');
// const devices = require('puppeteer/DeviceDescriptors');
// const iphone = devices["iPhone 6"]
const path = require('path');
// const pathToExtension = path.join(__dirname, './chrome-mac/Chromium.app/Contents/MacOS/Chromium');

const conf = {
  headless: false,
  // executablePath: pathToExtension,
  // defaultViewport: {
  //   width: 1300,
  //   height: 900
  // }
  defaultViewport: null,
  args: ['--start-maximized'],
  ignoreDefaultArgs: ['--enable-automation'],
  devtools: true
}
let cookieString = `BIDUPSID=57EACD60EBD0668909F3790431D3881F; PSTM=1608950406; BAIDUID=57EACD60EBD06689465CD14DB29EDC74:FG=1; BD_HOME=1; H_PS_PSSID=1453_33226_33306_33256_31254_33351_33313_33312_33169_33311_33310_33309_33321_33308_33307_22160_33389_33381_33370; BAIDUID_BFESS=57EACD60EBD06689465CD14DB29EDC74:FG=1; BD_UPN=123253; BA_HECTOR=a98l8ha08h0g2h84al1fud8k70q; BDUSS=1Z6QUNiS056RnU0WW5yaEZ0ckJXREJFYjFUMEN2eHlYVS1DVjh3bFU5V1dMdzVnSVFBQUFBJCQAAAAAAAAAAAEAAABa73tRS2F5bGFfzfUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJai5l-WouZfQ; BDUSS_BFESS=1Z6QUNiS056RnU0WW5yaEZ0ckJXREJFYjFUMEN2eHlYVS1DVjh3bFU5V1dMdzVnSVFBQUFBJCQAAAAAAAAAAAEAAABa73tRS2F5bGFfzfUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJai5l-WouZfQ`
const addCookies = async (cookies_str, page, domain) => {
  let cookies = cookies_str.split(';').map(
    pair => {
      let name = pair.trim().slice(0, pair.trim().indexOf('='));
      let value = pair.trim().slice(pair.trim().indexOf('=') + 1);
      return {name, value, domain}
    });
  await Promise.all(cookies.map(pair => {
    console.log(pair);
    return page.setCookie(pair)
  }))
};
(async () => {
  const browser = await puppeteer.launch(conf);

  const page = await browser.newPage();
  ps= await browser.pages()
  await ps[0].close()

  //await page.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36");
  // await page.emulate(iphone)
  await page.evaluateOnNewDocument(() => {
    delete navigator.__proto__.webdriver;
  });
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'plugins', {
      get: () => [1, 2, 3, 4, 5],
    });
  });
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'languages', {
      get: () => ['en-US', 'en'],
    });
  });
  await page.evaluateOnNewDocument(() => {
    Object.defineProperty(navigator, 'platform', {
      get: () => "Linux armxxxxx",
      configurable: true
    });
  });

  await page.evaluateOnNewDocument(() => {
    window.navigator.chrome = {
      runtime: {},
      getUserMedia: {},
    };
  });
  await addCookies(cookieString, page, 'www.baidu.com');

  await page.goto(`https://www.baidu.com`);

})()
